package cyou.breathe.blog.article.controller;

import cn.dev33.satoken.stp.StpUtil;
import cyou.breathe.blog.api.tag.model.ArticleTagVO;
import cyou.breathe.blog.api.tag.request.ArticleTagCreateRequest;
import cyou.breathe.blog.api.tag.request.ArticleTagDeleteRequest;
import cyou.breathe.blog.api.tag.request.ArticleTagQueryRequest;
import cyou.breathe.blog.api.tag.request.ArticleTagUpdateRequest;
import cyou.breathe.blog.api.tag.response.ArticleTagCreateResponse;
import cyou.breathe.blog.api.tag.response.ArticleTagDeleteResponse;
import cyou.breathe.blog.api.tag.response.ArticleTagQueryResponse;
import cyou.breathe.blog.api.tag.response.ArticleTagUpdateResponse;
import cyou.breathe.blog.article.domain.entity.ArticleTag;
import cyou.breathe.blog.article.domain.entity.convertor.ArticleTagConvertor;
import cyou.breathe.blog.article.domain.service.ArticleTagService;
import cyou.breathe.blog.article.param.DeleteArticleTagParam;
import cyou.breathe.blog.article.param.UpdateArticleTagParam;
import cyou.breathe.blog.web.vo.Result;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: breathe
 * @createTime: 2025-09-04
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("articleTag")
public class ArticleTagController {
    @Autowired
    private ArticleTagService articleTagService;

    @PostMapping("createArticleTag")
    public Result<Long> createArticleTag(@RequestParam("tagName") String tagName) {
        long userId = StpUtil.getLoginIdAsLong();
        ArticleTagCreateRequest request = new ArticleTagCreateRequest(tagName, userId);

        ArticleTagCreateResponse response = articleTagService.createTag(request);
        if (!response.getSuccess()) {
            return Result.error(response.getErrorCode());
        }
        return Result.success(response.getTagId());
    }

    @PutMapping("updateArticleTag")
    public Result<Long> updateArticleTag(@RequestBody @Valid UpdateArticleTagParam updateArticleTagParam) {
        long userId = StpUtil.getLoginIdAsLong();
        ArticleTagUpdateRequest articleTagUpdateRequest = new ArticleTagUpdateRequest(updateArticleTagParam.getTagName(), updateArticleTagParam.getTagId(), userId);
        ArticleTagUpdateResponse response = articleTagService.updateTag(articleTagUpdateRequest);

        if (!response.getSuccess()) {
            return Result.error(response.getErrorCode());
        }

        return Result.success(response.getTagId());
    }

    @DeleteMapping("deleteArticleTag")
    public Result<Long> deleteArticleTag(@RequestBody @Valid DeleteArticleTagParam deleteArticleTagParam) {
        long userId = StpUtil.getLoginIdAsLong();
        ArticleTagDeleteRequest request = new ArticleTagDeleteRequest();
        request.setTagId(deleteArticleTagParam.getTagId());
        request.setUserId(userId);
        ArticleTagDeleteResponse response = articleTagService.deleteTag(request);

        if (!response.getSuccess()) {
            return Result.error(response.getErrorCode());
        }

        return Result.success(response.getTagId());
    }

    @GetMapping("queryAllTags")
    public Result<List<ArticleTagVO>> queryAllTags() {
        long userId = StpUtil.getLoginIdAsLong();
        ArticleTagQueryRequest request = new ArticleTagQueryRequest(userId);

        ArticleTagQueryResponse<List<ArticleTag>> response = articleTagService.queryAllTags(request);

        if (!response.getSuccess()) {
            return Result.error(response.getErrorCode());
        }
        return Result.success(response.getData().stream().map(ArticleTagConvertor.INSTANCE::mapToVO).toList());
    }
}